// scale_manager.cpp
#include "scale_manager.h"
#include <QGraphicsView>
#include <QRectF>

ScaleManager::ScaleManager(QObject *parent) : QObject(parent) {}

qreal ScaleManager::calculateScaleFactor(const QSize &windowSize) {
    qreal scaleFactor = qMin(windowSize.width() / 1000.0, windowSize.height() / 700.0);
    return qBound(0.5, scaleFactor, 2.0); // 限制缩放范围
}

void ScaleManager::scaleCharacter(QGraphicsPixmapItem *characterItem, const QString &characterPath, qreal scaleFactor) {
    if (!characterItem) return;
    QPixmap characterPixmap(characterPath);
    if (characterPixmap.isNull()) return;

    QSize scaledSize = characterPixmap.size() * scaleFactor * 0.25;
    characterItem->setPixmap(
        characterPixmap.scaled(scaledSize, Qt::KeepAspectRatio, Qt::SmoothTransformation)
    );
}

void ScaleManager::scaleClothes(
    DragablePixmapItem *upItem, DragablePixmapItem *downItem,
    DragablePixmapItem *dressItem, DragablePixmapItem *shoesItem,
    const std::function<ClothesInfo(DragablePixmapItem*)> &getInfoFunc,
    qreal scaleFactor) {

    qreal clothesScale = scaleFactor * 0.23;

    auto updateItem = [clothesScale, &getInfoFunc](DragablePixmapItem *item) {
        if (!item) return;
        ClothesInfo info = getInfoFunc(item);
        QPixmap pixmap(info.path);
        if (pixmap.isNull()) return;
        QSize scaledSize = pixmap.size() * clothesScale;
        item->setPixmap(pixmap.scaled(scaledSize, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    };

    updateItem(upItem);
    updateItem(downItem);
    updateItem(dressItem);
    updateItem(shoesItem);
}

void ScaleManager::scaleCombo(QGraphicsPixmapItem *comboItem, const QString &comboPath,
                            QGraphicsPixmapItem *characterItem, qreal scaleFactor) {
    if (!comboItem || comboPath.isEmpty()) return;
    QPixmap pixmap(comboPath);
    if (pixmap.isNull()) return;

    QSize scaledSize = pixmap.size() * scaleFactor * 0.26;
    comboItem->setPixmap(pixmap.scaled(scaledSize, Qt::KeepAspectRatio, Qt::SmoothTransformation));

    if (characterItem) {
        comboItem->setPos(characterItem->pos());
    }
}

void ScaleManager::centerCharacter(QGraphicsPixmapItem *characterItem, QGraphicsView *view)
{
    if (!characterItem || !view) return;

    QRectF viewRect = view->mapToScene(view->viewport()->rect()).boundingRect();
    QRectF characterRect = characterItem->boundingRect();

    QPointF centerPos(
        viewRect.center().x() - characterRect.width() / 2,
        viewRect.center().y() - characterRect.height() / 2 + 180
    );
    characterItem->setPos(centerPos);
}

void ScaleManager::updateClothesPosition(
    DragablePixmapItem *upItem, DragablePixmapItem *downItem,
    DragablePixmapItem *dressItem, DragablePixmapItem *shoesItem,
    const std::function<ClothesInfo(DragablePixmapItem*)> &getInfoFunc,
    QGraphicsPixmapItem *characterItem)
{

    if (!characterItem) return;
    QPointF characterPos = characterItem->pos();

    auto updatePos = [&](DragablePixmapItem *item)
    {
        if (!item || !item->isWorn()) return;
        ClothesInfo info = getInfoFunc(item);
        item->setPos(characterPos + info.wearOffset);
    };

    updatePos(upItem);
    updatePos(downItem);
    updatePos(dressItem);
    updatePos(shoesItem);
}
